<template>
  <loading v-if="loading"></loading>
  <div v-else>
    <div class="top-box">
      <el-button type="warning" @click="addShow">添加配置项</el-button>
    </div>
    <div class="pd">
      <div class="search-box">
        <el-input v-model="key" placeholder="key" class="user-input"></el-input>
        <el-input v-model="tag" placeholder="tag" class="user-input"></el-input>
        <el-button type="primary" class="el-icon-search" @click="search">搜索</el-button>
        <el-button class="el-icon-q" @click="repeal">撤销</el-button>
      </div>
      <el-table :data="configlist.list" border fit highlight-current-row style="width:100%">
        <el-table-column label="ID" prop="id" width="120"></el-table-column>
        <el-table-column label="key" prop="key"></el-table-column>
        <el-table-column label="tag" prop="tag"></el-table-column>
        <el-table-column label="value" prop="value"></el-table-column>
        <el-table-column label="操作" width="120">
          <template scope="scope">
            <i title="修改" class="i-icon icon-revamp" @click="update(scope.row)"></i>
            <i title="删除" class="i-icon icon-delete" @click="del(scope.row.id)"></i>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @current-change="handleChange"
          :current-page.sync="pageall"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="configlist.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="添加配置项" :visible.sync="add.configAddShow">
      <div class="input-group">
        <label>key：</label>
        <div class="input-group-box"><el-input v-model="add.key" placeholder="输入key"></el-input></div>
      </div>
      <div class="input-group">
        <label>tag：</label>
        <div class="input-group-box"><el-input v-model="add.tag" placeholder="输入tag"></el-input></div>
      </div>
      <div class="input-group">
        <label>value：</label>
        <div class="input-group-box"><el-input v-model="add.value" type="textarea" :rows="5" placeholder="输入value"></el-input></div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="add.configAddShow = false">取 消</el-button>
        <el-button type="primary" @click="addConfig">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="修改配置项" :visible.sync="revamp.configShow">
      <div class="input-group">
        <label>key：</label>
        <div class="input-group-box"><el-input v-model="revamp.key" placeholder="输入key"></el-input></div>
      </div>
      <div class="input-group">
        <label>tag：</label>
        <div class="input-group-box"><el-input v-model="revamp.tag" placeholder="输入tag"></el-input></div>
      </div>
      <div class="input-group">
        <label>value：</label>
        <div class="input-group-box"><el-input type="textarea" :rows="5" v-model="revamp.value" placeholder="输入value"></el-input></div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="revamp.configShow = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<style>
</style>
<script>
import config from '../../utils/config'
import crypt from '../../utils/crypto-helper-pkcs7'
import axios from 'axios'
import loading from '../../components/loading'
export default {
  data () {
    return {
      key: '',
      tag: '',
      configlist: [],
      pageSize: 20,
      pageall: 1,
      page: 1,
      loading: true,
      add: {
        configAddShow: false,
        key: '',
        tag: '',
        value: ''
      },
      revamp: {
        configShow: false,
        key: '',
        tag: '',
        value: '',
        id: 0
      }
    }
  },
  created () {
    this.config()
  },
  components: {
    loading
  },
  methods: {
    config () {
      var param = {
        key: this.key,
        tag: this.tag,
        page: this.page
      }
      var paramjson = JSON.stringify(param)
      var params = crypt.encrypt(paramjson)
      axios.get(config.domain + config.urls.configList + '?param=' + params)
      .then(res => {
        var response = res.data
        if (response.errcode === 200) {
          this.configlist = response.data
          this.loading = false
        } else if (response.errcode === 200103) {
          this.$confirm('登录过期，请重新登录')
          .then(_ => {
            window.sessionStorage.clear()
            this.$router.push('/admin/login')
          })
          .catch(_ => {})
        }
      })
      .catch(error => {
        console.log(error)
      })
    },
    handleChange (val) {
      this.page = val
      this.config()
    },
    repeal () {
      this.key = ''
      this.tag = ''
    },
    search () {
      this.config()
    },
    addShow () {
      this.add.configAddShow = true
    },
    addConfig () {
      var param = {
        key: this.add.key,
        tag: this.add.tag,
        value: this.add.value
      }
      var paramjson = JSON.stringify(param)
      var params = crypt.encrypt(paramjson)
      axios.get(config.domain + config.urls.configAdd + '?param=' + params)
      .then(res => {
        var response = res.data
        if (response.errcode === 200) {
          this.$notify({
            title: '成功',
            message: '添加成功',
            type: 'success',
            duration: 1500
          })
          this.add.configAddShow = false
          this.add.key = ''
          this.add.tag = ''
          this.add.value = ''
          this.page = 1
          this.config()
        } else if (response.errcode === 200103) {
          this.$confirm('登录过期，请重新登录')
          .then(_ => {
            window.sessionStorage.clear()
            this.$router.push('/admin/login')
          })
          .catch(_ => {})
        }
      })
      .catch(error => {
        console.log(error)
      })
    },
    del (id) {
      var param = {
        id: id
      }
      var paramjson = JSON.stringify(param)
      var params = crypt.encrypt(paramjson)
      this.$confirm('确认要删除此条信息吗？')
      .then(_ => {
        axios.get(config.domain + config.urls.configDelete + '?param=' + params)
        .then(res => {
          var response = res.data
          if (response.errcode === 200) {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 1500
            })
            this.config()
          } else if (response.errcode === 200103) {
            this.$confirm('登录过期，请重新登录')
            .then(_ => {
              window.sessionStorage.clear()
              this.$router.push('/admin/login')
            })
            .catch(_ => {})
          } else {
            this.$notify({
              title: '失败',
              message: '删除失败',
              type: 'error',
              duration: 1500
            })
          }
        })
        .catch(error => {
          console.log(error)
        })
      })
      .catch(_ => {})
    },
    update (data) {
      this.revamp.id = data.id
      this.revamp.configShow = true
      this.revamp.key = data.key
      this.revamp.tag = data.tag
      this.revamp.value = data.value
    },
    save () {
      var param = {
        id: this.revamp.id,
        key: this.revamp.key,
        tag: this.revamp.tag,
        value: this.revamp.value
      }
      var paramjson = JSON.stringify(param)
      var params = crypt.encrypt(paramjson)
      axios.get(config.domain + config.urls.configUpdate + '?param=' + params)
      .then(res => {
        var response = res.data
        if (response.errcode === 200) {
          this.$notify({
            title: '成功',
            message: '修改成功',
            type: 'success',
            duration: 1500
          })
          this.config()
          this.revamp.id = 0
          this.revamp.configShow = false
          this.revamp.key = ''
          this.revamp.tag = ''
          this.revamp.value = ''
        } else if (response.errcode === 200103) {
          this.$confirm('登录过期，请重新登录')
          .then(_ => {
            window.sessionStorage.clear()
            this.$router.push('/admin/login')
          })
          .catch(_ => {})
        } else {
          this.$notify({
            title: '失败',
            message: '修改失败',
            type: 'error',
            duration: 1500
          })
        }
      })
      .catch(error => {
        console.log(error)
      })
    }
  }
}
</script>
<style>
.search-box {
  font-size: 0;
  padding-bottom:10px;
 }
 .search-box .user-input{
   width: 197px;
   margin: 3px 2px;
 }
</style>
